import {PluginCommand} from "./PluginCommand";
import {getCurrentPartMaterialInfoApi} from "../util/request/materialLibraryApi";

class SoundInsulationWeightCommand extends PluginCommand{
  constructor() {
    super();
  }

  handlePickInstance(cb){
    this.event.subscribeInstancePickEvent(res => (
      cb(res.instance)
    ));
  }

  async getInstanceTree() {
    if(this.query) {
      let instanceList = [];
      let res = await this.query.getInstanceTree();
      let instanceTree = res?.data?.instanceTree;
      if (instanceTree && instanceTree.length > 0) {
        for (let i = 0; i < instanceTree.length; i++) {
          this.getPartInstance(instanceTree[i], instanceList);
        }
      }
      return instanceList;
    }
  }
  getPartInstance(instance, instanceList) {
    let children = instance?.children;
    if (children && children.length > 0) {
      for (let i = 0; i < children.length; i++) {
        this.getPartInstance(children[i], instanceList);
      }
    } else {
      instanceList.push(instance);
    }
  }

  getEntities(cb) {
    if(this.query) {
      this.query.getEntities().then(res => {
        cb(res?.data?.entities);
      });
    }
  }

  async getInstanceProtoRefByInstanceId(params, cb) {
    if (this.toolQuery) {
      let res = await this.toolQuery.getInstanceProtoRefByInstanceId(params);
      return res?.data?.instance;
    }
  }

  async getPartInfo() {
    this.openLoading("正在计算......");
    let resData = [];
    let res = await this.toolQuery.getCurrentPartDocInfo();
    let document = res?.data?.document;
    if (document && document.type === "PartDocument") {
      let variables = await this.getVariableValues();
      let formData = new FormData();
      formData.append('partDocId',document.id ?? "");
      let materialRes = await getCurrentPartMaterialInfoApi(formData);
      let materialInfo = materialRes.data;
      resData.push({
        name: document.name,
        variables: variables,
        materials: materialInfo,
      })
    } else {
      let instances = await this.getInstanceTree();
      for (let i = 0; i < instances.length; i++) {
        let instanceId = instances[i]?.id;
        let docId = instances[i]?.protoRefDocId;
        let result = await this.toolQuery.getAllVariablesByInstanceId(instanceId);
        if (result?.data?.data) {
          let variables = await this.getInstanceVariables(result?.data?.data);
          let formData = new FormData();
          formData.append('partDocId',docId ?? "");
          let materialRes = await getCurrentPartMaterialInfoApi(formData);
          let materialInfo = materialRes.data;
          resData.push({
            name: instances[i].name,
            variables: variables,
            materials: materialInfo,
          })
        }
      }
    }
    this.closeLoading();
    return resData;
  }

  async getVariableValues() {
    // let aa = await this.command.query.getVariableValueIncrement({variableName: "W1"});
    let allVarRes = await this.command.query.getAllVariableIncrement();
    let dataArray = allVarRes?.data?.modelData?.data;
    return await this.getInstanceVariables(dataArray);
  }

  async getInstanceVariables(dataArray) {
    let varW1Value = this.getVariableValue(dataArray, "'W1'");
    let varL1Value = this.getVariableValue(dataArray, "'L1'");
    let varL3Value = this.getVariableValue(dataArray, "'L3'");
    let varA1Value = this.getVariableValue(dataArray, "'A1'");
    let varA2Value = this.getVariableValue(dataArray, "'A2'");
    // //将单位毫米换算成米
    // if (varW1Value) {
    //   varW1Value = parseFloat(varW1Value) / 1000;
    // }
    // if (varL1Value) {
    //   varL1Value = parseFloat(varL1Value) / 1000;
    // }
    // if (varL3Value) {
    //   varL3Value = parseFloat(varL3Value) / 1000;
    // }
    // if (varA1Value) {
    //   varA1Value = parseFloat(varA1Value) / 1000000;
    // }
    // if (varA2Value) {
    //   varA2Value = parseFloat(varA2Value) / 1000000;
    // }
    let variables = {
      W1: varW1Value,
      L1: varL1Value,
      L3: varL3Value,
      A1: varA1Value,
      A2: varA2Value,
    }
    return variables;
  }

  getVariableValue(dataArray, variableName) {
    let variable = dataArray?.filter(data => data.variableName == variableName);
    if (variable && variable.length > 0) {
      return variable[0].variableValue;
    }
  }
}

const soundInsulationWeightCommand = new SoundInsulationWeightCommand();
export {soundInsulationWeightCommand};
